package GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import registration.ClientRegistration;

@SuppressWarnings("serial")
public class RegisterPanel extends JPanel {

	private JLabel lblEmail;
	private JTextField txtEmail;
	private JLabel lblResult;
	private JButton btnSubmit;
	private JButton btnLogin;
	private JButton btnActivate;

	public RegisterPanel(){
		initializePanel();
	}

	private void initializePanel() {
		this.setLayout(null);

		lblEmail = new JLabel("Email");
		lblEmail.setBounds(100, 100, 40, 30);
		lblEmail.setVisible(true);
		this.add(lblEmail);

		txtEmail = new JTextField("");
		txtEmail.setBounds(240 , 100 , 140 , 30);
		txtEmail.setVisible(true);
		this.add(txtEmail);

		lblResult = new JLabel("");
		lblResult.setBounds(100, 170, 400, 30);
		lblResult.setVisible(true);
		this.add(lblResult);

		btnSubmit = new JButton("Register");
		btnSubmit.setBounds(100, 250, 100, 30);
		btnSubmit.setVisible(true);
		btnSubmit.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				submitButton();
			}

		});
		this.add(btnSubmit);
		
		btnLogin = new JButton("Back");
		btnLogin.setBounds(220, 250, 100, 30);
		btnLogin.setVisible(true);
		btnLogin.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				backToLogin();
				
			}

		});
		this.add(btnLogin);

	}

	private void backToLogin(){
		MainFrame.getMainFrame().remove(this);
		MainFrame.getMainFrame().addLoginPanel();
	}
	private void GotoActivatePanel(){
		ActivatePanel activatePnl = new ActivatePanel();
		MainFrame.getMainFrame().remove(this);
		MainFrame.getMainFrame().add(activatePnl);
		activatePnl.repaint();
		MainFrame.getMainFrame().repaint();
		MainFrame.getMainFrame().validate();
	}
	
	private void submitButton() {
		ClientRegistration clientReg = new ClientRegistration();
		String email = txtEmail.getText();

		if(!clientReg.checkEmailFormat(email)){
			lblResult.setText("Please enter a valid student email");
			return;
		}
		boolean success = clientReg.register(email);
		if(!success){
			lblResult.setText("Email already exists. Please login with your password");
		}
		else{
			GotoActivatePanel();
		}
	}
}
